/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.PhoneCodeStoreDao;
import com.techshop.model.PhoneCodeStore;
import com.techshop.model.PhoneCodeStoreId;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class PhoneCodeStoreDaoImpl extends HibernateDaoSupport implements PhoneCodeStoreDao
{
    @Override
    public PhoneCodeStore getById(PhoneCodeStoreId phoneCodeStoreId)
    {
        try
        {
            return (PhoneCodeStore) getSession().get(PhoneCodeStore.class, phoneCodeStoreId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
    @Override
    public PhoneCodeStore getByPhoneCodeAndStore(int phoneCodeId, int storeId)
    {
        try
        {
            return (PhoneCodeStore) getSession().createQuery("FROM PhoneCodeStore WHERE phoneCode.id=:phoneCodeId AND store.id=:storeId").setInteger("phoneCodeId", phoneCodeId).setInteger("storeId", storeId).list().get(0);
        }
        catch (DataAccessResourceFailureException | IndexOutOfBoundsException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
    @Override
    public List<PhoneCodeStore> getAll()
    {
        try
        {
            return getSession().createQuery("FROM PhoneCodeStore").list();
        }
        catch (DataAccessResourceFailureException | IndexOutOfBoundsException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
    @Override
    public boolean update(PhoneCodeStore phoneCodeStore)
    {
        try
        {
            if (getById(phoneCodeStore.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(phoneCodeStore));
                ValidatePhonePubSubClient.updatePhone(phoneCodeStore.getPhone().getId());
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean insert(PhoneCodeStore phoneCodeStore)
    {
        try
        {
            getHibernateTemplate().save(phoneCodeStore);
            ValidatePhonePubSubClient.updatePhone(phoneCodeStore.getPhone().getId());
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public List<PhoneCodeStore> search(int storeId, int quantity)
    {
        try
        {
            if (storeId != 0)
            {
                return getSession().createQuery("FROM PhoneCodeStore WHERE store.id=:storeId AND quantity<=:quantity").setInteger("storeId", storeId).setInteger("quantity", quantity).list();
            }
            else
            {
                return getSession().createQuery("FROM PhoneCodeStore WHERE quantity<=:quantity").setInteger("quantity", quantity).list();
            }
        }
        catch (DataAccessResourceFailureException | IndexOutOfBoundsException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
}